package com.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.portlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/demo")
public class HelloController {
    public HelloController()
    {
        System.out.println("=------------------ init spring controller --------");
    }

    @RequestMapping(value="/hello", method=RequestMethod.GET)
    public ModelAndView hello(HttpServletRequest req, HttpServletResponse res)
    {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("id", req.getParameter("id"));

        return mv;
    }

    @RequestMapping(value="/ajax", method=RequestMethod.GET)
    @ResponseBody
    public String ajax(HttpServletRequest req, HttpServletResponse res)
    {
        return "{\"a\":1, \"b\":"+ System.currentTimeMillis() +"}";
    }

    @RequestMapping(value="/hello", method=RequestMethod.POST)
    public ModelAndView hello222(HttpServletRequest req, HttpServletResponse res)
    {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "I'm from SpringMVC Controller");

        return mv;
    }

    @RequestMapping("/say")
    public ModelAndView say(HttpServletRequest req, HttpServletResponse res)
    {
        ModelAndView mv = new ModelAndView("say");
        mv.addObject("name", "yswang");

        return mv;
    }
}
